package net.wangds.redis.comp;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.wangds.common.base.funcs.Functions;
import net.wangds.log.helper.LogHelper;
import net.wangds.spring.utils.SpringUtils;
import org.slf4j.Logger;

import java.io.Closeable;
import java.io.IOException;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/6/17 10:08.
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CloseableLock implements Closeable {

    @Getter
    private String key;

    @Getter
    private String value;

    @Override
    public void close() throws IOException {

        Functions.tryRun(()->{
            if (!((RedisLock) SpringUtils.getBean("redisLock")).unlock(key, value)) {
                throw new RuntimeException("解锁不成功，"+key+","+value);
            }
        }, 3, LogHelper::warn);
    }
}
